/**
 * PlaginInfo.java
 */
package com.pluginsacceptor.acceptor.ui.info;

import java.awt.Color;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import org.apache.log4j.Logger;

import com.pluginsacceptor.acceptor.activator.PluginList;
import com.pluginsacceptor.acceptor.activator.model.PluginInfo;
import com.pluginsacceptor.acceptor.ui.RootPlaginWindow;

/**
 * Represent plugins info
 */
public class PluginInfoFrame {

    /** */
    public static int DEL_BUTTON_POS = 2;

    /** Logger */
    private static final Logger LOG = Logger.getLogger(PluginInfoFrame.class);

    /** */
    private static final long serialVersionUID = 4266346575800148981L;



    public static Frame showPluginInfo(final RootPlaginWindow rootPlaginWindow) {


        final JFrame frame = new JFrame("Plugin info");
        frame.setSize(800, 200);
        frame.setBackground(Color.GRAY);

        final JTable table = new JTable();
        final TableModel model = refillTableModel();
        table.setModel(model);
        table.setDefaultRenderer(JButton.class, new PluginInfoRenderer(table));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.addMouseListener(new JTableButtonMouseListener(table, rootPlaginWindow));

        final JScrollPane scrollPane = new JScrollPane(table);

        frame.add(scrollPane);
//        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        frame.setAlwaysOnTop(true);
        frame.setVisible(true);
        return frame;
    }

    /**
     * Fill plugin info
     * @return TableModel
     */
    public static TableModel refillTableModel() {
        return new WindowTableModel();
    }

    private static class JTableButtonMouseListener extends MouseAdapter {
        /** */
        private final JTable table;

        /** */
        private final RootPlaginWindow rootPlaginWindow;

        public JTableButtonMouseListener(final JTable table, final RootPlaginWindow rootPlaginWindow) {
            this.table = table;
            this.rootPlaginWindow = rootPlaginWindow;
        }

        @Override
        public void mouseClicked(final MouseEvent e) {
            final int column = table.getColumnModel().getColumnIndexAtX(e.getX());
            final int row  = e.getY() / table.getRowHeight();

            if (row < table.getRowCount() && row >= 0 && column == 2) {
                final Object value = table.getValueAt(row, column);
                if (value instanceof JButton) {
                    final PluginInfo pluginInfo = PluginList.getActivePlugins().get(row);
                    LOG.debug("removed row " + row);
                    if (PluginList.isLinkedPluginExist(pluginInfo)) {
                        JOptionPane.showMessageDialog(
                          null,
                          "Can not remove plugin. You should delete linked plugins.",
                          "Warning",
                          JOptionPane.WARNING_MESSAGE
                       );
                    } else {
                        ((JButton)value).doClick();
                        this.rootPlaginWindow.removePluginFromUI(row);

                        table.repaint();
                    }
                }
            }
        }
    }


    /**
     * Constructor
     */
    private PluginInfoFrame() {
        super();
    }
}
